<template>

	<view>
		<view class="lineH2"></view>
		<view class="uni-padding-wrap uni-common-mt">
			<input class="uni-input" placeholder="当前密码" password :value="oldpassword" @input="oldpasswordChange" />
			<view class="lineH1"></view>

			<input class="uni-input" placeholder="新密码" password :value="newpassword" @input="newpasswordChange" />
			<view class="lineH1"></view>

			<input class="uni-input" placeholder="确认新密码" password :value="confirmpassword" @input="confirmpasswordChange" />
			<view class="lineH1"></view>

			<button type="primary" @tap="updatePass">修改密码</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldpassword: '',
				newpassword: '',
				confirmpassword: ''
			}
		},
		methods: {
			oldpasswordChange: function(e) {
				this.oldpassword = e.target.value
			},
			newpasswordChange: function(e) {
				this.newpassword = e.target.value
			},
			confirmpasswordChange: function(e) {
				this.confirmpassword = e.target.value
			},
			updatePass() {
				const userinfo = getApp().globalData.userInfo;
				let _oldpass = this.oldpassword,
					_newpass = this.newpassword,
					_confirmpass = this.confirmpassword;
				if (_oldpass.length == 0) {
					uni.showToast({
						title: '请输入正确的原密码',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (_newpass.length == 0) {
					uni.showToast({
						title: '请输入新密码',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (_newpass != _confirmpass) {
					uni.showToast({
						title: '两次密码输入不一致',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				this.$http.get('ac/xagrid/appUserService/updatePassword', {
					oldPassword: _oldpass,
					newPassword: _newpass,
					confirmNewPassword: _confirmpass
				}).then((res) => {
					if (res.errcode === '0') { //0成功  1失败
						let isRemind = false; //是否存储了密码
						let username = '';
						uni.getStorage({
							key: 'userAccount',
							success(res) {
								let password = res.data.pass;
								username = res.data.name;
								if (password.length == 0) {
									isRemind = false;
								} else {
									isRemind = true;
								}
							},
							fail(res) {

							}
						});
						var savePas = isRemind === true ? _newpass : '';

						var tempData = {
							name: username,
							pass: savePas
						};
						uni.setStorage({//重新存储数据
							key: "userAccount",
							data: tempData,
							success() {}
						});
						uni.showToast({
							title: res.errmsg,
							icon: 'success',
							duration: 1000
						});
						uni.navigateBack();
// 						uni.reLaunch({
// 							url: "../../../pages/login/login"
// 						});
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none',
							duration: 1500
						});
					}
				});
			}
		}
	}
</script>

<style>
	.lineH1 {
		height: 2upx;
		width: 100%;
		/* margin-left: 10upx; */
		background-color: #F7F7F7;
	}

	.lineH2 {
		height: 32upx;
		width: 100%;
		background-color: #F7F7F7;
	}

	button {
		margin-top: 32upx;
	}
</style>
